package org.cbg.btn;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.cbg.bean.GoodsBackBean;
import org.cbg.frame.TrslationMainJframe;
import org.cbg.panel.TraslationMyMainMygoodsJpanel;
import org.cbg.panel.TraslationMyMainMygoodsModelJpanel;
import org.come.Frame.ZhuFrame;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;

import com.tool.btn.MoBanBtn;

public class TraslationMyMainMygoodsBtn extends MoBanBtn {

    private int caozuo;
    private TraslationMyMainMygoodsJpanel traslationMyMainMygoodsJpanel;

    public TraslationMyMainMygoodsBtn(String iconpath, int type, int caozuo,
            TraslationMyMainMygoodsJpanel traslationMyMainMygoodsJpanel) {
        super(iconpath, type);
        // TODO Auto-generated constructor stub
        this.caozuo = caozuo;
        this.traslationMyMainMygoodsJpanel = traslationMyMainMygoodsJpanel;
    }


    //    图片解密



    public TraslationMyMainMygoodsBtn(String iconpath, int type, Color[] colors, Font font, int caozuo, String text,
            TraslationMyMainMygoodsJpanel traslationMyMainMygoodsJpanel) {
        super(iconpath, type, colors);
        // TODO Auto-generated constructor stub
        this.caozuo = caozuo;
        setText(text);
        setFont(font);
        setHorizontalTextPosition(SwingConstants.CENTER);
        setVerticalTextPosition(SwingConstants.CENTER);
        this.traslationMyMainMygoodsJpanel = traslationMyMainMygoodsJpanel;
    }



    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        TrslationMainJframe trslationMainJframe = TrslationMainJframe.getTrslationMainJframe();
        if (caozuo == 1) {

            // TODO Auto-generated method stub

            // 返回背包剩余格数
            int packNumber = GoodsListFromServerUntil.Surplussum("-1", "-1", 999);
            // 判断背包格数是否剩余
            if (packNumber >= trslationMainJframe.getGoodsGouxuangeshu()) {
                /**
                 * 初始化发送对象List<消息表ID> 获取勾选个数以及对应的消息表Id
                 */
                GoodsBackBean backBean = new GoodsBackBean();
                List<BigDecimal> mesIdList = new ArrayList<>();
                JPanel view = (JPanel) traslationMyMainMygoodsJpanel.getjScrollPane().getViewport().getView();
                Component[] components = view.getComponents();
                for (int i = 0; i < components.length; i++) {
                    JPanel panel = (JPanel) components[i];
                    JLabel component2 = (JLabel) panel.getComponent(3);
                    if ("0".equals(component2.getName())) {
                        JLabel component3 = (JLabel) panel.getComponent(0);
                        BigDecimal mesId = new BigDecimal(component3.getName());
                        mesIdList.add(mesId);
                    }
                }
                if (mesIdList.size() == 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有选择要取回的物品");
                    return;
                }
                backBean.setIds(mesIdList);
                // 修改勾选总数以及当前勾选个数
                trslationMainJframe.setGoodsGeshuMax(trslationMainJframe.getGoodsGeshuMax()
                        - trslationMainJframe.getGoodsGouxuangeshu());
                trslationMainJframe.setGoodsGouxuangeshu(0);
                /**
                 * 藏宝阁我的货物之取回货物CBGBack//{"ids":[40,38,10,14,33,33,33,28,13]}
                 * 转换为对应协议的json字符串 提交给服务器处理
                 */
                String sendmes = Agreement.getAgreement().searchGoodsBackAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(backBean));
                SendMessageUntil.toServer(sendmes);

                /**
                 * 修改物品的状态
                 */
                for (int i = 0; i < components.length; i++) {
                    JPanel panel = (JPanel) components[i];
                    JLabel component2 = (JLabel) panel.getComponent(3);
                    if ("0".equals(component2.getName())) {
                        JLabel component3 = (JLabel) panel.getComponent(5);
                        component3.setText("已取回");
                        JLabel gouxuan = (JLabel) panel.getComponent(3);
                        gouxuan.setName("1");
                        gouxuan.setIcon(null);
                        gouxuan.setVisible(false);
                        panel.getComponent(4).setVisible(false);
                        panel.getComponent(6).setVisible(false);
                    }
                }
                traslationMyMainMygoodsJpanel.getGouxuan().setIcon(null);
                traslationMyMainMygoodsJpanel.getGouxuan().setName("1");
            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("背包格数不足");
            }

            /**
             * 调用回调方法
             */
            // MapAction.serverAction.get(AgreementUtil.CBGMes).controlMessFromServer(mes);

        } else if (caozuo == 2) {

            // TODO Auto-generated method stub
            if (traslationMyMainMygoodsJpanel.getDangqianyeshu() != 1) {
                traslationMyMainMygoodsJpanel.setDangqianyeshu(1);
                traslationMyMainMygoodsJpanel.getYema().setText(
                        traslationMyMainMygoodsJpanel.getDangqianyeshu() + "/"
                                + traslationMyMainMygoodsJpanel.getZuidayema());
                /**
                 * 传递数据获取我的藏宝阁我的货物中的数据信息 1、获取角色ID 2、获取当前页数(点击我的货物按钮页数默认为1)
                 * 3、转换为对应协议的json对象 4、切换跳转页面编号为9 5、发送给服务器处理数据信息
                 */
                String sendmes = Agreement.getAgreement().searchMyGoodsAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(traslationMyMainMygoodsJpanel.getDangqianyeshu()));
                trslationMainJframe.setPanelOpen(9);
                SendMessageUntil.toServer(sendmes);

            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("已在首页");
            }

        } else if (caozuo == 3) {

            // TODO Auto-generated method stub
            if (traslationMyMainMygoodsJpanel.getDangqianyeshu() != traslationMyMainMygoodsJpanel.getZuidayema()) {
                traslationMyMainMygoodsJpanel.setDangqianyeshu(traslationMyMainMygoodsJpanel.getZuidayema());
                traslationMyMainMygoodsJpanel.getYema().setText(
                        traslationMyMainMygoodsJpanel.getDangqianyeshu() + "/"
                                + traslationMyMainMygoodsJpanel.getZuidayema());

                /**
                 * 传递数据获取我的藏宝阁我的货物中的数据信息 1、获取角色ID 2、获取当前页数(点击我的货物按钮页数默认为1)
                 * 3、转换为对应协议的json对象 4、切换跳转页面编号为9 5、发送给服务器处理数据信息
                 */
                String sendmes = Agreement.getAgreement().searchMyGoodsAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(traslationMyMainMygoodsJpanel.getDangqianyeshu()));
                trslationMainJframe.setPanelOpen(9);
                SendMessageUntil.toServer(sendmes);
            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("已在末页");
            }

        } else if (caozuo == 4) {
            // TODO Auto-generated method stub
            if (traslationMyMainMygoodsJpanel.getDangqianyeshu() > 1) {
                traslationMyMainMygoodsJpanel.setDangqianyeshu(traslationMyMainMygoodsJpanel.getDangqianyeshu() - 1);
                traslationMyMainMygoodsJpanel.getYema().setText(
                        traslationMyMainMygoodsJpanel.getDangqianyeshu() + "/"
                                + traslationMyMainMygoodsJpanel.getZuidayema());
                /**
                 * 传递数据获取我的藏宝阁我的货物中的数据信息 1、获取角色ID 2、获取当前页数(点击我的货物按钮页数默认为1)
                 * 3、转换为对应协议的json对象 4、切换跳转页面编号为9 5、发送给服务器处理数据信息
                 */
                String sendmes = Agreement.getAgreement().searchMyGoodsAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(traslationMyMainMygoodsJpanel.getDangqianyeshu()));
                trslationMainJframe.setPanelOpen(9);
                SendMessageUntil.toServer(sendmes);
            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("已经没有上一页了");
            }
        } else if (caozuo == 5) {
            // TODO Auto-generated method stub
            if (traslationMyMainMygoodsJpanel.getDangqianyeshu() < traslationMyMainMygoodsJpanel.getZuidayema()) {
                traslationMyMainMygoodsJpanel.setDangqianyeshu(traslationMyMainMygoodsJpanel.getDangqianyeshu() + 1);
                traslationMyMainMygoodsJpanel.getYema().setText(
                        traslationMyMainMygoodsJpanel.getDangqianyeshu() + "/"
                                + traslationMyMainMygoodsJpanel.getZuidayema());
                /**
                 * 传递数据获取我的藏宝阁我的货物中的数据信息 1、获取角色ID 2、获取当前页数(点击我的货物按钮页数默认为1)
                 * 3、转换为对应协议的json对象 4、切换跳转页面编号为9 5、发送给服务器处理数据信息
                 */
                String sendmes = Agreement.getAgreement().searchMyGoodsAgreement(
                        GsonUtil.getGsonUtil().getgson().toJson(traslationMyMainMygoodsJpanel.getDangqianyeshu()));
                trslationMainJframe.setPanelOpen(9);
                SendMessageUntil.toServer(sendmes);
            } else {
                ZhuFrame.getZhuJpanel().addPrompt2("已经没有下一页了");
            }
        }
    }
}
